package com.ambulance.project.manager.service.impl;

import com.ambulance.common.exception.CustomException;
import com.ambulance.common.utils.DateUtils;
import com.ambulance.project.biz.entity.Car;
import com.ambulance.project.biz.entity.CarDevice;
import com.ambulance.project.biz.service.CarDeviceService;
import com.ambulance.project.biz.service.CarService;
import com.ambulance.project.manager.domain.BizCardevice;
import com.ambulance.project.manager.mapper.BizCardeviceMapper;
import com.ambulance.project.manager.service.IBizCardeviceService;
import com.ambulance.utils.CommonUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.List;

/**
 * 车载设备管理Service业务层处理
 *
 * @author zr
 * @date 2020-05-28
 */
@Service
public class BizCardeviceServiceImpl implements IBizCardeviceService {
    @Autowired
    private BizCardeviceMapper bizCardeviceMapper;
    @Autowired
    private CarService carService;
    @Autowired
    private CarDeviceService carDeviceService;

    /**
     * 查询车载设备管理
     *
     * @param deviceID 车载设备管理ID
     * @return 车载设备管理
     */
    @Override
    public BizCardevice selectBizCardeviceById(Long deviceID) {
        return bizCardeviceMapper.selectBizCardeviceById(deviceID);
    }


    /**
     * 查询车载设备调度管理
     *
     * @param deviceID 车载设备管理ID
     * @return 车载设备管理
     */
    @Override
    public List<BizCardevice> selectBizCardeviceDisaptchLogById(Long deviceID) {
        final List<BizCardevice> bizCardevices = bizCardeviceMapper.selectBizCardeviceDispatchById(deviceID);
        if(!CommonUtils.checkNull(bizCardevices)){
            for (BizCardevice item : bizCardevices){
                item.setCaseFlow(item.getFlow());
                if(!CommonUtils.checkNull(item.getFlow())){
                    item.setCaseFlow(item.getFlow().substring(0,item.getFlow().length()-4));
                }
                if(!CommonUtils.checkNull(item.getStartTime())){
                    Duration duration = Duration.between(DateUtils.parseString(item.getStartTime()), DateUtils.parseString(item.getEndTimes()));
                  // 分钟数 改为秒数
//                    Long minutes = duration.toMinutes();
                    Long seconds = duration.getSeconds();
                    item.setTimeQuantum(seconds.toString());
                }
            }
        }
        return bizCardevices;
    }
    /**
     * 查询车载设备管理列表
     *
     * @param bizCardevice 车载设备管理
     * @return 车载设备管理
     */
    @Override
    public List<BizCardevice> selectBizCardeviceList(BizCardevice bizCardevice) {
        if (!CommonUtils.checkNull(bizCardevice.getLastDate())) {
            String lastDate = bizCardevice.getLastDate();
            String[] split = lastDate.split(",");
            bizCardevice.setLastDates(split[0]);
            bizCardevice.setLastDatee(split[1]);
        }
        return bizCardeviceMapper.selectBizCardeviceList(bizCardevice);
    }

    /**
     * 新增车载设备管理
     *
     * @param bizCardevice 车载设备管理
     * @return 结果
     */
    @Override
    public int insertBizCardevice(BizCardevice bizCardevice) {
        // 判断serial是否重复
        if (!CommonUtils.checkNull(bizCardevice.getCarID())) {
            final Car byId = carService.getById(bizCardevice.getCarID());
            bizCardevice.setCarVin(byId.getVin());
            //todo  判断车载设备绑定的车辆
            final BizCardevice bizCardevice1 = new BizCardevice();
            bizCardevice1.setCarID(bizCardevice.getCarID());
            bizCardevice1.setType(1);
            final List<BizCardevice> bizCardevices1 = this.selectBizCardeviceList(bizCardevice1);
            if (bizCardevice.getType().equals(1) && !CommonUtils.checkNull(bizCardevices1) && !bizCardevice1.getCarID().equals(bizCardevice.getCarID())) {
                throw new CustomException("该车辆已绑定心电设备");
            }
            bizCardevice1.setType(2);
            final List<BizCardevice> bizCardevices2 = this.selectBizCardeviceList(bizCardevice1);
            if (bizCardevice.getType().equals(2) && !CommonUtils.checkNull(bizCardevices2) && !bizCardevice1.getCarID().equals(bizCardevice.getCarID())) {
                throw new CustomException("该车辆已绑定呼吸机");
            }
            bizCardevice1.setType(3);
            final List<BizCardevice> bizCardevices3 = this.selectBizCardeviceList(bizCardevice1);
            if (bizCardevice.getType().equals(3) && !CommonUtils.checkNull(bizCardevices3) && !bizCardevice1.getCarID().equals(bizCardevice.getCarID())) {
                throw new CustomException("该车辆已绑定飞利浦超声设备");
            }
            bizCardevice1.setType(4);
            final List<BizCardevice> bizCardevices4 = this.selectBizCardeviceList(bizCardevice1);
            if (bizCardevice.getType().equals(4) && !CommonUtils.checkNull(bizCardevices4) && !bizCardevice1.getCarID().equals(bizCardevice.getCarID())) {
                throw new CustomException("该车辆已绑定汇声超声设备");
            }
        }
        final CarDevice serial = carDeviceService.getOne(new QueryWrapper<CarDevice>().eq("serial", bizCardevice.getSerial()));
        if(!CommonUtils.checkNull(serial)){
            throw new CustomException("序列号已存在");
        }
        return bizCardeviceMapper.insertBizCardevice(bizCardevice);
    }

    /**
     * 修改车载设备管理
     *
     * @param bizCardevice 车载设备管理
     * @return 结果
     */
    @Override
    public int updateBizCardevice(BizCardevice bizCardevice) {

        if (!CommonUtils.checkNull(bizCardevice.getCarID())) {
            final Car byId = carService.getById(bizCardevice.getCarID());
            bizCardevice.setCarVin(byId.getVin());
            //todo  判断车载设备绑定的车辆
            final BizCardevice bizCardevice1 = new BizCardevice();
            bizCardevice1.setCarID(bizCardevice.getCarID());
            bizCardevice1.setType(1);
            final List<BizCardevice> bizCardevices1 = this.selectBizCardeviceList(bizCardevice1);
            if (bizCardevice.getType().equals(1) && !CommonUtils.checkNull(bizCardevices1) && !bizCardevice1.getCarID().equals(bizCardevice.getCarID())) {
                throw new CustomException("该车辆已绑定心电设备");
            }
            bizCardevice1.setType(2);
            final List<BizCardevice> bizCardevices2 = this.selectBizCardeviceList(bizCardevice1);
            if (bizCardevice.getType().equals(2) && !CommonUtils.checkNull(bizCardevices2) && !bizCardevice1.getCarID().equals(bizCardevice.getCarID())) {
                throw new CustomException("该车辆已绑定呼吸机");
            }
            bizCardevice1.setType(3);
            final List<BizCardevice> bizCardevices3 = this.selectBizCardeviceList(bizCardevice1);
            if (bizCardevice.getType().equals(3) && !CommonUtils.checkNull(bizCardevices3) && !bizCardevice1.getCarID().equals(bizCardevice.getCarID())) {
                throw new CustomException("该车辆已绑定飞利浦超声设备");
            }
            bizCardevice1.setType(4);
            final List<BizCardevice> bizCardevices4 = this.selectBizCardeviceList(bizCardevice1);
            if (bizCardevice.getType().equals(4) && !CommonUtils.checkNull(bizCardevices4) && !bizCardevice1.getCarID().equals(bizCardevice.getCarID())) {
                throw new CustomException("该车辆已绑定汇声超声设备");
            }
        }
        final CarDevice serial = carDeviceService.getOne(new QueryWrapper<CarDevice>().eq("serial", bizCardevice.getSerial()));
        if(!CommonUtils.checkNull(serial) && !serial.getDeviceID().equals(bizCardevice.getDeviceID().intValue())){
            throw new CustomException("序列号已存在");
        }
        return bizCardeviceMapper.updateBizCardevice(bizCardevice);
    }

    /**
     * 批量删除车载设备管理
     *
     * @param deviceIDs 需要删除的车载设备管理ID
     * @return 结果
     */
    @Override
    public int deleteBizCardeviceByIds(Long[] deviceIDs) {
        return bizCardeviceMapper.deleteBizCardeviceByIds(deviceIDs);
    }

    /**
     * 删除车载设备管理信息
     *
     * @param deviceID 车载设备管理ID
     * @return 结果
     */
    @Override
    public int deleteBizCardeviceById(Long deviceID) {
        return bizCardeviceMapper.deleteBizCardeviceById(deviceID);
    }
}
